package com.zq.it.dao.impl.admin;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.zq.it.dao.admin.CourseDao;
import com.zq.it.dao.impl.BaseDao;
import com.zq.it.pojo.Course;

public class CourseDaoImpl extends BaseDao<Course> implements CourseDao{
  /*public static final Logger logger=t(CourseDaoImpl.class);*/
	@Override
	public void saveCourseEty(Course course){
    	getHibernateTemplate().save(course);
	}
	@Override
	public void deleteCourseEtyByID(Integer id){	
		deleteEntity("delete Course as c where c.courseId="+id);
	}

	@Override
	public List<Course> findCourseEtyList(){
		return getHibernateTemplate().find("from Course");
		// TODO Auto-generated method stub
	
	}
	public Course findCourseFetchL(Integer id){
//		if(id==null)
//			return null;O
//		Session session=getSession();
	   Course course= (Course)getSession().load(Course.class,id);
	   Set set=course.getChapters();
	  
		
		return course;
	}

	@Override
	public List findCourseEtyById(Integer id) {
		return getHibernateTemplate().find("from Course where courseId=?",id);
		// TODO Auto-generated method stu
	}

	@Override
	public List findCourseEtyByName(String name) {
		return getHibernateTemplate().find("from Course as c where c.courseName=?",name);
		// TODO Auto-generated method stub
	
	}

//	@Override
//	public void updateCourseByID(Integer id) {
//		getHibernateTemplate().update(entity);
//		// TODO Auto-generated method stub
//		
//	}

	@Override
	public void updateCourseEty(Course course) {
		getHibernateTemplate().update(course);
		// TODO Auto-generated method stub
		
	}
	@Override
	public List findCourseEtyByDate(Date date){
		// TODO Auto-generated method stub
		return getHibernateTemplate().find("from Course where createTime=?",date);
	}
	@Override
	public List findCourseEtyByExmp(Course course) {
		// TODO Auto-generated method stub
		return getHibernateTemplate().findByExample(course);
	}
	@Override
	public void deleteAllEty(java.util.Collection<Course> collection){
		getHibernateTemplate().deleteAll(collection);
	}
	
	@Override
	public void deleteCourseEtyByObj(Course course){
		getHibernateTemplate().delete(course);
	}
	@Override
	public void saveCourseEtyList(Collection<Course> collection){
		getHibernateTemplate().saveOrUpdateAll(collection);
	}
	@Override
	public List findCourseEtyByHQL(String hql){
		// TODO Auto-generated method stub
		return findEtyByHql(hql);
	}
	@Override
	public List findCourseNameAndId() {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().executeFind(new HibernateCallback(){
			@Override
			public List doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				return arg0.createSQLQuery("select courseId,courseName from Course").list() ;
			}
			
			
		});
	}
	
}
